const BaseService = require("./baseService");
const MailUtil=require("../utils/MailUtil");

class CustomerInfoService extends BaseService {
    constructor(){
        super(...arguments);
        this.tableName="customerinfo";
    }
    
    async getListByPage({customername,customersex,customernickname,customerstatus,pageIndex}={}){
        pageIndex=pageIndex||1;
        let strWhere="";
        if(customername){
            strWhere+=" and customername like '%"+customername+"%'";
        }
        if(customersex){
            strWhere+=" and customersex ='"+customersex+"'";
        }
        if(customernickname){
            strWhere+=" and customernickname like '%"+customernickname+"%'";
        }
        if(customerstatus){
            strWhere+=" and customerstatus ='"+customerstatus+"'";
        }
        let strSql=`select * from ${this.tableName} where isDel=false ${strWhere} limit ?,? `;
        let countSql=this.createCountSql(strWhere);
        let result =await this.app.mysql.query(strSql+";"+countSql,[(pageIndex-1)*this.pageSize,this.pageSize]);
        return result;
    }

    /**
     * 新增用户，相当于注册信息
     */
    async addCustomerInfo({customername,customersex,customertel,customermail,customerpwd,customerphoto,customernickname}){
        let result = await this.app.mysql.insert(this.tableName,{
            customername,customersex,customertel,customermail,customerpwd,customerphoto,customernickname
        });
        let flag = result.affectedRows>0?true:false;
        if(flag){
            let mailUtil=new MailUtil();
            //注意，此处转在敢异步请求，不用等待邮件的发送结果
            mailUtil.sendMail(customermail,"欢迎注册Softeem商城",`
            尊敬的${customernickname}用户：
                您好！
                欢迎注册Softeem商城，你的账号是${customername},注册时间为${new Date().toLocaleString()}
                请妥善保存好您的密码，勿泄露给其它人！
                如非本人注册，请忽略!祝您生活愉快
                                                    Softeem商城
                                                    ${new Date().toLocaleString()}
            `);
            
        }
        return flag;
    }
    async checkCustomerName(customername){
        let result = await this.app.mysql.get(this.tableName,{customername,isDel:false});
        return result;
    }

    /**
     * 检测登陆
     * @param {customername,adminpwd} param 登陆的账号
     * @description 注意此处的账号可能是手机号，也可以是邮箱 
     */
    async checkLogin({customername,customerpwd}){
        let strWhere=" and customerpwd=? ";
        if(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(customername)){
            //说明是手机号
            strWhere+=" and customertel=? ";
        }
        else if(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(customername)){
            //说明是邮箱
            strWhere+=" and customermail=? ";
        }
        else{
            strWhere+=" and customername=? ";
        }
        let strSql=`select * from customerinfo where isDel=false ${strWhere} `;
        let result = await this.app.mysql.query(strSql,[customerpwd,customername]);
        return result;
    }

   
}

module.exports=CustomerInfoService;